/**
 * 区域组件,不含区域权限
 * @用法 <div id="divArea"></div>
 * @作者 田应平
 * @创建时间 2021-11-16 15:11
 * @QQ号码 444141300
 * @官网 http://www.fwtai.com
*/
;(function($){
    var clsProvince = 'clsProvince';//省|市,级别1
    var clsCity = 'clsCity';//地州市,级别2
    var clsCounty = 'clsCounty';//区|县,级别3
    var clsTowns = 'clsTowns';//乡|镇|社区,级别4
    var clsVallage = 'clsVallage';//村|居委会,级别5
    var clsStreet = 'clsStreet';//组|街道,级别6
    var limitLevel = 'limitLevel';//默认限制显示的级别数
    window.selectGroup = {
        /**
            用法 : selectGroup.getListData(url,'0','#divArea',3,'选择省市','clsProvince');||selectGroup.getListData(url,'0','#divArea',3,'选择省市');||selectGroup.getListData(url,'0','#divArea',3);
            @param url 请求的url
            @param pid 父级节点
            @param container 容器的节点,'#Xxxx'或'.Xxxx',div的容器
            @param limit 限制级别,如1的话仅显示省市;2仅显示到市区;3仅显示到区|县
            @param spanText 默认显示的文字,仅未选择未选中时显示的文字,默认可不填
            @param targetClass 下一个的class,默认仅能 clsProvince,默认可不填则是 clsProvince
        */
        getListData : function(url,pid,container,limit,spanText,targetClass){
            $(container).attr(limitLevel,limit);
            targetClass = (targetClass) ? targetClass : clsProvince
            spanText = (spanText) ? spanText : '选择省市'
            var _self = this;
            self.layerIndex = layerFn.loading('正在加载……');
            $.ajax({
                type : "GET",
                url : urlPrefix + url,
                dataType : "json",
                data : {pId : pid},
                headers : {'accessToken': sessionStorage.getItem('accessToken') || '',"refreshToken":sessionStorage.getItem('refreshToken') || ''},//好使
                crossDomain: true == !(document.all),
                success : function(data){
                    if(data.code == AppKey.code.code200){
                        _self.renderNode(data.data,container,targetClass,spanText,url,limit);
                    }else if(data.code == AppKey.code.code205){
                        layerFn.tokenLogin();
                    }
                },
                statusCode : {
                    401 : function(response){
                        layerFn.handleClose("没有操作权限");
                    },
                    404 : function(response){
                        var json = eval('('+ response.responseText +')');
                        layerFn.handleClose("请求("+json.path+")路径不存在");
                    },
                    500 : function(response){
                        layerFn.handleClose("系统出现错误,稍候重试");
                    },
                    502 : function(response){
                        layerFn.handleClose("网关代理出错,联系管理员");
                    }
                },
                complete : function(response,status){
                    layerFn.closeIndex(self.layerIndex);
                }
            });
        },
        //赋值和置空
        changValue : function(url,pid,container,limit,targetClass,text){
            this.setLabelText(container,targetClass,text);
            if(pid){
                this.setInputValue(container,targetClass,pid);
                var spanText = '';
                switch (targetClass){
                    case clsProvince:
                        spanText = '选择省|市';
                        break;
                    case clsCity:
                        spanText = '选择市|县';
                        break;
                    case clsCounty:
                        spanText = '选择区|县';
                        break;
                    case clsTowns:
                        spanText = '乡|镇|社区';
                        break;
                    case clsVallage:
                        spanText = '村|居委会';
                        break;
                    case clsStreet:
                        spanText = '组|街道';
                        break;
                    default:
                        break;
                }
                if(spanText){
                    var level = $(container).attr(limitLevel);
                    if(level == 1){
                        if(targetClass == clsCity) return;
                    }
                    if(level == 2){
                        if(targetClass == clsCounty) return;
                    }
                    if(level == 3){
                        if(targetClass == clsTowns) return;
                    }
                    if(level == 4){
                        if(targetClass == clsVallage) return;
                    }
                    if(level == 5){
                        if(targetClass == clsStreet) return;
                    }
                    this.getListData(url,pid,container,limit,spanText,targetClass);
                }
            }else{
                this.nodeRemove(container,targetClass);
                this.resetValue(container,targetClass);
            }
        },
        renderNode : function(data,container,targetClass,spanText,url,limit){
            var exit = $(container +" div."+targetClass+" input").length > 0;
            if (exit){
                this.nodeRemove(container,targetClass);
            }
            var html = this.createNode(data,container,targetClass,spanText,url,limit);
            $(container).append(html);
        },
        /*移除节点*/
        nodeRemove : function(container,targetClass){
            switch (targetClass){
                case clsProvince ://移除或隐藏全部
                    this.nodeDisplayHide(container,clsProvince+','+clsCity+','+clsCounty+','+clsTowns+','+clsVallage+','+clsStreet);
                    break;
                case clsCity:
                    this.nodeDisplayHide(container,clsCity+','+clsCounty+','+clsTowns+','+clsVallage+','+clsStreet);
                    break;
                case clsCounty:
                    this.nodeDisplayHide(container,clsCounty+','+clsTowns+','+clsVallage+','+clsStreet);
                    break;
                case clsTowns:
                    this.nodeDisplayHide(container,clsTowns+','+clsVallage+','+clsStreet);
                    break;
                case clsVallage:
                    this.nodeDisplayHide(container,clsVallage+','+clsStreet);
                    break;
                case clsStreet:
                    this.nodeDisplayHide(container,clsStreet);
                    break;
                default:
                    break;
            }
        },
        /*置空*/
        resetValue : function(container,targetClass){
            var cls = '';
            switch (targetClass){
                case clsProvince:
                    break;
                case clsCity:
                    cls = clsProvince;
                    break;
                case clsCounty:
                    cls = clsCity;
                    break;
                case clsTowns:
                    cls = clsCounty;
                    break;
                case clsVallage:
                    cls = clsTowns;
                    break;
                case clsStreet:
                    cls = clsVallage;
                    break;
                default:
                    break;
            }
            if(cls){
                $(container +" div."+cls+" input").val('');
            }
        },
        /*赋值*/
        setInputValue : function(container,targetClass,value){
            var cls = '';
            switch (targetClass){
                case clsProvince:
                    break;
                case clsCity:
                    cls = clsProvince;
                    break;
                case clsCounty:
                    cls = clsCity;
                    break;
                case clsTowns:
                    cls = clsCounty;
                    break;
                case clsVallage:
                    cls = clsTowns;
                    break;
                case clsStreet:
                    cls = clsVallage;
                    break;
                default:
                    break;
            }
            if(cls){
                $(container +" div."+cls+" input").val(value);
            }
        },
        /*设置显示的文字*/
        setLabelText : function(container,targetClass,text){
            var cls = '';
            switch (targetClass){
                case clsProvince:
                    break;
                case clsCity:
                    cls = clsProvince;
                    break;
                case clsCounty:
                    cls = clsCity;
                    break;
                case clsTowns:
                    cls = clsCounty;
                    break;
                case clsVallage:
                    cls = clsTowns;
                    break;
                case clsStreet:
                    cls = clsVallage;
                    break;
                default:
                    break;
            }
            $(container +" div."+cls+" span").text(text);
        },
        /*移除节点*/
        nodeDisplayHide : function(container,arrNode){
            arrNode = arrNode.split(',');
            for(var i=0;i<arrNode.length;i++){
                var element = arrNode[i];
                $(container + ' div.'+element).remove();
            }
        },
        createNode : function(data,container,targetClass,spanText,url,limit){
            var marginLeft = -2;
            if(targetClass == clsProvince){
                marginLeft = 0;
            }
            var target = '';
            switch (targetClass){
                case clsProvince:
                    target = clsCity;
                    break;
                case clsCity:
                    target = clsCounty;
                    break;
                case clsCounty:
                    target = clsTowns;
                    break;
                case clsTowns:
                    target = clsVallage;
                    break;
                case clsVallage:
                    target = clsStreet;
                    break;
                default:
                    break;
            }
            if(!target){
                return;
            }
            var html = '';
            html += '<div class="btn-group '+targetClass+'" style="margin-left:'+marginLeft+'px;">';
            html += '<button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle">';
            html += '<span>'+spanText+'</span>';
            html += '<i class="ace-icon fa fa-angle-down icon-on-right"></i>';
            html += '</button>';
            html += '<ul class="dropdown-menu">';
            html += '<li><a href="javascript:selectGroup.changValue(\''+url+'\',\'\',\''+container+'\','+limit+',\''+target+'\',\''+spanText+'\');">'+spanText+'</a></li>';
            $.each(data,function(index,item){
                html += '<li><a rel="'+item.id+'" href="javascript:selectGroup.changValue(\''+url+'\',\''+item.id+'\',\''+container+'\','+limit+',\''+target+'\',\''+item.name+'\');">'+item.name+'</a></li>';
            });
            html += '</ul>';
            html += '<input type="hidden"/>';
            html += '</div>';
            return html;
        }
    };
})(jQuery);